Thank you for watching!
Dive into Deep Learning
Train a deep learning model to classify beetles, cockroaches and dragonflies using these images. Note: Original images from https://www.insectimages.org/index.cfm. Blog about this, and explain how the neural network classified the images using SHapley Additive exPlanations.
If DropBox does not work, you can use this wget https://people.duke.edu/~ccc14/insects.zip
For this blog (project), we want to train a deep learning model to classify images of different insects (beetles, cockroaches, dragonflies)
The neural network and its classification we used will be explained by Shapley Additive Explanations.
We first import all the libraries we want to use for this project.
Out neural network is used under tensorflow/keras.
Other libraries we used include but not limited to: matplotlib, numpy, skimage etc.
%matplotlib inline
import tensorflow as tf
import matplotlib as plt
import numpy as np
import cv2
import os
import PIL
from PIL import Image
import glob
import skimage
from skimage import data
from matplotlib import pyplot as plt
%pylab inline
from tensorflow.keras import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
import pickle
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
img=image.load_img("insects/train/beetles/5556579.jpg")
plt.imshow(img)
plt.show(img)
cv2.imread("insects/train/beetles/5556636.jpg").shape
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rotation_range=15,
rescale=1./255,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split=0.2) # set validation split
train_dataset = train_datagen.flow_from_directory("insects/train",
#shuffle=True,
target_size= (200,200),
batch_size = 3,
class_mode = 'categorical',
subset='training')
validation_dataset = train_datagen.flow_from_directory("insects/train",
#shuffle=True,
target_size= (200,200),
batch_size = 3,
class_mode = 'categorical',
subset='validation'
)
test_datagen = ImageDataGenerator(rotation_range=15,
rescale=1./255,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
width_shift_range=0.1,
height_shift_range=0.1)
test_generator = test_datagen.flow_from_directory("insects/train",#shuffle=True,
target_size=(200,200),
class_mode='categorical',
batch_size=3)
train_dataset.class_indices
validation_dataset.class_indices
Cool, looks like our 3 classes are: beetles as 0, cockroach as 1, and dragonflies as 2!
Let's do something for our model! For this model, we used a neural network that has a convolution 2D neural net structure.
How do we build it?
We declare different layers sequentially so that every layer has its shape and dimensions. We then dense and flatten the layers.
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(32,(3,3),activation='relu',input_shape=(200,200,3)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3),activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512,activation='relu'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(3,activation='softmax')
])
model.compile(loss='categorical_crossentropy',
optimizer = RMSprop(lr=0.001),
metrics=['accuracy']
)
history=model.fit(train_dataset,
steps_per_epoch = 5,
batch_size = 1,
epochs = 30,
validation_data = validation_dataset
)
loss_train = history.history['loss']
loss_val = history.history['val_loss']
epochs = range(1,31)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
loss_train = history.history['accuracy']
loss_val = history.history['val_accuracy']
epochs = range(1,31)
plt.plot(epochs, loss_train, 'g', label='Training accuracy')
plt.plot(epochs, loss_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
model.summary()
dir_path = 'insects/test/beetles'
for i in os.listdir(dir_path):
img= image.load_img(dir_path+'/'+i, target_size=(200,200))
plt.imshow(img)
plt.show()
X=image.img_to_array(img)
X= np.expand_dims(X, axis=0)
images = np.vstack([X])
val=model.predict(images)
if (val==0).any():
print("It is a beetle")
else:
print("Not a beetle")
x_train=np.concatenate([train_dataset.next()[0] for i in range(train_dataset.__len__())])
y_train=np.concatenate([train_dataset.next()[1] for i in range(train_dataset.__len__())])
x_test=np.concatenate([test_generator.next()[0] for i in range(test_generator.__len__())])
y_test=np.concatenate([test_generator.next()[1] for i in range(test_generator.__len__())])
y_test = np.where(ytest == 0, "beetles", np.where(ytest == 1, "cockroach", "dragonflies"))
explainer = shap.GradientExplainer(model, xtrain)
shap_vals = explainer.shap_values(xtest[:5])
shap.image_plot([shap_vals[i] for i in range(3)], xtest[91:96])